package com.lucaswangdev.biz.service.impl;

import com.lucaswangdev.biz.manager.WorkOrderManager;
import com.lucaswangdev.biz.request.workOrder.WorkOrderCreateRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderDeleteRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderQueryRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderUpdateRequest;
import com.lucaswangdev.biz.response.common.PagingResponse;
import com.lucaswangdev.biz.service.WorkOrderService;
import com.lucaswangdev.constant.common.ErrorConstants;
import com.lucaswangdev.exception.BusinessException;
import com.lucaswangdev.model.convert.WorkOrderConverter;
import com.lucaswangdev.model.domain.WorkOrderParam;
import com.lucaswangdev.model.dto.WorkOrderDTO;
import com.lucaswangdev.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Resource
    private WorkOrderManager workOrderManager;

    @Override
    public Boolean create(WorkOrderCreateRequest workOrderCreateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(workOrderCreateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        WorkOrderDTO workOrderDTO = WorkOrderConverter.INSTANCE.convertRequest2DTO(workOrderCreateRequest);
        workOrderManager.create(workOrderDTO);
        return Boolean.TRUE;
    }

    @Override
    public PagingResponse<WorkOrderDTO> listPage(WorkOrderQueryRequest queryRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(queryRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        WorkOrderParam param = new WorkOrderParam();
        Integer currentPage;
        Integer pageSize;
        if (queryRequest.getCurrentPage() != null) {
            currentPage = queryRequest.getCurrentPage();
        } else {
            currentPage = 1;
        }
        if (queryRequest.getPageSize() != null) {
            pageSize = queryRequest.getPageSize();
        } else {
            pageSize = 20;
        }
        param.setCurrentPage(currentPage);
        param.setPageSize(pageSize);
        Long count = workOrderManager.countByParam(param);
        if (count == null || count == 0) {
            return PagingResponse.build(Lists.newArrayList(), 0L, queryRequest.getCurrentPage(), queryRequest.getPageSize());
        }
        List<WorkOrderDTO> workOrderDTOS = workOrderManager.queryByParam(param);
        return PagingResponse.build(workOrderDTOS, count, queryRequest.getCurrentPage(), queryRequest.getPageSize());
    }

    @Override
    public Boolean modified(WorkOrderUpdateRequest workOrderUpdateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(workOrderUpdateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        WorkOrderDTO updateWorkOrderDTO = WorkOrderConverter.INSTANCE.convertRequest2DTO(workOrderUpdateRequest);
        workOrderManager.update(updateWorkOrderDTO);
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteById(WorkOrderDeleteRequest workOrderDeleteRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(workOrderDeleteRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        if(workOrderDeleteRequest.getId() != null) {
            workOrderManager.deleteById(workOrderDeleteRequest.getId());
        }
        return Boolean.TRUE;
    }
}
